package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.SensitiveDto;

import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.AdSensitive;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;


@Service
@Transactional //开启事务

public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {



    //敏感词查询功能
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //判断参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断分页数据是否为空
        dto.checkParam();
        //根据数据 模糊查询
        //设置分页数据
        Page page = new Page(dto.getPage(),dto.getSize());
        //判断模糊查询参数
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }

        //查询到数据
        IPage result = page(page,lambdaQueryWrapper);
        //封装返回数据
        ResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    //
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //判断参数
        if(adSensitive==null||adSensitive.getSensitives()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行方法
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        //返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //修改敏感词汇
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //判断参数
        if(adSensitive==null){
          return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行方法
        updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //删除敏感词汇
    @Override
    public ResponseResult delete(Integer id) {
        //判断参数
        if(id==null){
          return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询根据ID
        AdSensitive adSensitive = getById(id);
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
